<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <!-- <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item> -->
      <el-form-item label="信息" prop="additional">
        <!-- <el-input v-model="dataForm.additional" placeholder="额外字段 JSOn"></el-input> -->

        <el-button
          icon="el-icon-plus"
          size="small"
          type="primary"
          @click="addHeader()"
          >添加信息</el-button
        >
        <!-- 动态增加项目 -->
        <!-- 不止一个项目，用div包裹起来 -->
        <div v-for="(item, index) in testInfo.headers" :key="index">
          <div class="div-inline distance_style_10">
            <el-form-item style="margin-bottom: 20px">
              <el-input
                style="width: 100px"
                type="text"
                name="StartTime"
                v-model="item.title"
              />

              <el-input
                v-model="item.headerVal"
                style="width: 200px"
              ></el-input>
              <el-input v-model="item.show" style="width: 200px"></el-input
              >
              <span style="color:red">1显示0隐藏</span>


                     <el-button @click.prevent="removeHeader(item, index)"
                >删除</el-button
              >
            </el-form-item>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="经纬度" prop="latitude">
        <el-input v-model="dataForm.latitude" placeholder="经纬度"></el-input>
      </el-form-item>
      <el-form-item label="企业详细信息" prop="info">
        <!-- <el-input v-model="dataForm.info" placeholder="企业详细信息"></el-input> -->

        <div id="editor"></div>
      </el-form-item>

      <!-- <el-form-item label="商户Id" prop="shopInfo">
      <el-input v-model="dataForm.shopInfo" placeholder="商户Id"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import Editor from "wangeditor";
export default {
  data() {
    return {
      uploadUrledit: this.$http.adornUrl(
        `/testing/file/uploadedit?token=${this.$cookie.get("token")}`
      ),
      visible: false,
      dataForm: {
        id: 0,
        createTime: "",
        updateTime: "",
        latitude: "",
        info: "",
        additional: "",
        shopInfo: "",
      },
      testInfo: {
        //del==false 不可删除
        headers: [{ title: "联系人", headerVal: "xxxxxx", show: "1" }],
      },
      dataRule: {
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: true, message: "修改时间不能为空", trigger: "blur" },
        ],
        latitude: [
          { required: true, message: "经纬度不能为空", trigger: "blur" },
        ],
        info: [
          { required: true, message: "企业详细信息不能为空", trigger: "blur" },
        ],
        additional: [
          { required: true, message: "额外字段 JSOn不能为空", trigger: "blur" },
        ],
        shopInfo: [
          { required: true, message: "商户Id不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    addHeader() {
      this.testInfo.headers.push({
        title: "联系人",
        headerVal: "xxxxx",
        show: "1",
      });
    },
    removeHeader(item, index) {
      this.testInfo.headers.splice(index, 1);
    },

    beforeDestroy() {
      // 销毁编辑器
      if (this.editor != null) {
        this.editor.destroy();
        this.editor = null;
      }
    },
    getHtml() {
      return this.editor.txt.html();
    },
    initedit() {
      this.editor = new Editor("#editor");
      // 配置菜单
      // 限制类型
      // editor.config.uploadImgAccept = ['jpg', 'jpeg', 'png', 'gif', 'bmp']
      this.editor.config.uploadImgMaxLength = 1; // 一次最多上传 1 个图片
      this.editor.config.showLinkImg = false; //关闭网络路径图片方式
      this.editor.config.uploadFileName = "file"; // formdata中的name属性
      // this.editor.config.uploadImgShowBase64 = true;
      this.editor.config.uploadImgServer = this.uploadUrledit;
      // 配置菜单栏，删减菜单，调整顺序
      this.editor.config.menus = [
        "undo", // 撤销
        "redo", // 重复
        "head", // 标题
        "bold", // 粗体
        "fontSize", // 字号
        "fontName", // 字体
        "italic", // 斜体
        "foreColor", // 文字颜色
        "backColor", // 背景颜色
        "justify", // 对齐方式
        "image", // 插入图片
      ];
      this.editor.create();
    },
    init(id) {
    
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.beforeDestroy();
        this.initedit();
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/testing/shopinfo/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.createTime = data.shopinfo.createTime;
              this.dataForm.updateTime = data.shopinfo.updateTime;
              this.dataForm.latitude = data.shopinfo.latitude;
              this.dataForm.info = data.shopinfo.info;
              this.dataForm.additional = data.shopinfo.additional;
              this.dataForm.shopInfo = data.shopinfo.shopInfo;
              this.testInfo = JSON.parse(data.shopinfo.additional);
              this.editor.txt.html(this.dataForm.info); // 重新设置编辑器内容
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.dataForm.info = this.getHtml();
      this.dataForm.additional = JSON.stringify(this.testInfo); //自定义表单
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/testing/shopinfo/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              latitude: this.dataForm.latitude,
              info: this.dataForm.info,
              additional: this.dataForm.additional,
              shopInfo: this.dataForm.shopInfo,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
